#!/usr/bin/env python
# encoding: utf-8

from tqdm import tqdm
import numpy as np
from .compute_eer import compute_eer

def cosine_score(trials, index_mapping, eval_vectors):
    target_scores = []
    nontarget_scores = []
    for item in trials:
        enroll_vector = eval_vectors[index_mapping[item[1]]]
        test_vector = eval_vectors[index_mapping[item[2]]]
        score = enroll_vector.dot(test_vector.T)
        denom = np.linalg.norm(enroll_vector) * np.linalg.norm(test_vector)
        score = score/denom
        if item[0] == "1":
            target_scores.append(score)
        else:
            nontarget_scores.append(score)
    eer, th = compute_eer(target_scores, nontarget_scores)
    return eer, th

def PLDA_score(trials, index_mapping, eval_vectors, plda_analyzer):
    target_scores = []
    nontarget_scores= []
    for item in trials:
        enroll_vector = eval_vectors[index_mapping[item[1]]]
        test_vector = eval_vectors[index_mapping[item[2]]]
        score = plda_analyzer.NLScore(enroll_vector, test_vector)
        if item[0] == "1":
            target_scores.append(score)
        else:
            nontarget_scores.append(score)
    eer, th = compute_eer(target_scores, nontarget_scores)
    return eer, th



